package com.pospi.pospihold.ui;

import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.kyleduo.switchbutton.SwitchButton;
import com.pospi.pospihold.App;
import com.pospi.pospihold.R;
import com.pospi.pospihold.print.a920.PrnTest;
import com.pospi.pospihold.ui.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PrintSettingActivity extends BaseActivity {

    @Bind(R.id.get_connect_printer)
    SwitchButton getConnectPrinter;
    @Bind(R.id.printer_state)
    TextView printerState;
    @Bind(R.id.ll_printer_state)
    LinearLayout llPrinterState;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_print_setting);
        setTitle("打印机设置");
        ButterKnife.bind(this);
        getConnectPrinter.setChecked(App.IsPrinter);
        getConnectPrinter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                App.IsPrinter = isChecked;
                SharedPreferences.Editor editor = getSharedPreferences("IsPrinter", MODE_PRIVATE).edit();
                editor.putBoolean("IsPrinter", App.IsPrinter);
                editor.apply();

                if (isChecked) {
                    llPrinterState.setAlpha((float) 1);
                } else {
                    llPrinterState.setAlpha((float) 0);
                }
            }
        });

        byte status = PrnTest.prnStatus();
        showStatus(status);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void showStatus(byte status) {
        switch (status) {
            case 0x00:
                printerState.setText("打印机可用");
                break;
            case 0x01:
                printerState.setText("打印机忙");
                break;
            case 0x02:
                printerState.setText("打印机缺纸");
                break;
            case 0x03:
                printerState.setText("打印数据包格式错");
                break;
            case 0x04:
                printerState.setText("打印机故障");
                break;
            case 0x08:
                printerState.setText("打印机过热");
                break;
            case 0x09:
                printerState.setText("打印机电压过低");
                break;
            case (byte) 0xf0:
                printerState.setText("打印未完成");
                break;
            case (byte) 0xfc:
                printerState.setText("打印机未装字库");
                break;
            case (byte) 0xfe:
                printerState.setText("数据包过长");
                break;
            case 97:
                printerState.setText("不支持的字符集");
                break;
            case 98:
                printerState.setText("参数为空");
                break;
            case 99:
                printerState.setText("RPC连接错误");
                break;
            default:
                printerState.setText("其它错误");
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "PrintSetting Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://com.pospi.pospihold.ui/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "PrintSetting Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://com.pospi.pospihold.ui/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}
